package com.wwwarehouse.carddesk.adapter.functionlogincontrol;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskGroupresponseBean;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : xiaofei.he
 *     time   : 2017/11/15
 *     desc   : 登录控制 列表
 *     version: 1.0
 * </pre>
 */
public class FunctionLoginControlAdapter extends RecyclerView.Adapter<FunctionLoginControlAdapter.AdapterViewholder> {
    private Context mContext;
    private int mItemHeight;
    private int mItemWidth;
    List<CardDeskGroupresponseBean.ArrayListBean> list = new ArrayList<>();

    private void caculateItemHeight() {
        int screenHeight = ScreenUtils.getScreenHeight(mContext);
        int screenWidth = ScreenUtils.getScreenWidth(mContext);
        int barHeight = ScreenUtils.getStatusHeight(mContext);
        mItemHeight = (screenHeight - barHeight - ConvertUtils.dip2px(mContext, 120)) / 7;
    }

    public FunctionLoginControlAdapter(Context mContext, StateOnclickListener listener, List<CardDeskGroupresponseBean.ArrayListBean> list) {
        this.mContext = mContext;
        this.listener = listener;
        this.list = list;
        caculateItemHeight();
    }

    @Override
    public AdapterViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_group_logincontrol_adapter, null);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.height = mItemHeight;
        view.setLayoutParams(params);

        return new AdapterViewholder(view);
    }

    @Override
    public void onBindViewHolder(AdapterViewholder holder, int position) {
        final CardDeskGroupresponseBean.ArrayListBean bean = list.get(position);
        holder.loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    if("1".equals(bean.getStatus() + "")){
                        listener.click(bean.getPersonId(),"0",bean.getPersonName());
                    }else{
                        listener.click(bean.getPersonId(),"1",bean.getPersonName());
                    }

                }
            }
        });
        BaseApplication.getApplicationInstance().displayImg(bean.getFaceUrl(),holder.headview);
        /**
         * status:1 正常，8被锁定
         */
        if("1".equals(bean.getStatus() + "")){
            holder.loginStateTxt.setText("" + mContext.getString(R.string.teamdesk_login_control_stoplogin_status_open));
            holder.loginBtn.setNormalBackgroundColor(mContext.getResources().getColor(R.color.common_color_c2_f7a82f));
            holder.loginBtn.setPressedBackgroundColor(mContext.getResources().getColor(R.color.common_color_c2_f7a82f));
            holder.loginBtn.setText("" + mContext.getString(R.string.teamdesk_login_control_stoplogin));
        }else{
            holder.loginStateTxt.setText("" + mContext.getString(R.string.teamdesk_login_control_stoplogin_status_close));
            holder.loginBtn.setText("" + mContext.getString(R.string.teamdesk_login_control_login));
            holder.loginBtn.setNormalBackgroundColor(mContext.getResources().getColor(R.color.blue_cc337cff));
            holder.loginBtn.setPressedBackgroundColor(mContext.getResources().getColor(R.color.blue_cc337cff));
        }

        holder.nameTxt.setText("" + bean.getPersonName());


    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class AdapterViewholder extends RecyclerView.ViewHolder {
        private CircleImageView headview;
        private TextView nameTxt;
        private TextView loginStateTxt;
        private StateButton loginBtn;

        public AdapterViewholder(View itemView) {
            super(itemView);
            headview = (CircleImageView) itemView.findViewById(R.id.group_adapter_imageview);
            nameTxt = (TextView) itemView.findViewById(R.id.group_adapter_nametxt);
            loginStateTxt = (TextView) itemView.findViewById(R.id.group_adapter_loginstatetxt);
            loginBtn = (StateButton) itemView.findViewById(R.id.group_adapter_statebutton);
        }
    }

    StateOnclickListener listener;

    public interface StateOnclickListener {
        void click(String personId,String type,String name);
    }
}
